﻿Imports Oracle.DataAccess.Client
Public Class fListaProductos
    Dim lIdArticulo As String
    Dim lestado As Int16 = 0
    Dim lItemBusca As String
    Dim lListaPrecios As String = ""
    Public Property ListaPrecios() As String
        Get
            Return lListaPrecios
        End Get
        Set(ByVal value As String)
            lListaPrecios = value
        End Set
    End Property
    Public ReadOnly Property idArticulo() As String
        Get
            Return lIdArticulo
        End Get
    End Property
    Public WriteOnly Property ItemBusca() As String
        Set(ByVal value As String)
            lItemBusca = value
        End Set
    End Property
    Public WriteOnly Property estado() As Int16
        Set(ByVal value As Int16)
            Select Case value
                Case 1
                    Me.rbActivos.Checked = True
                Case 2
                    Me.rbInactivos.Checked = True
                Case Else
                    Me.rbTodos.Checked = True
            End Select
        End Set
    End Property
    Private Sub Consultar()
        Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = conn.CreateCommand
        If lListaPrecios.Trim.Length > 0 Then
            cmd.CommandText = "SELECT * FROM PRO_PRODUCTO, PRO_DETALLELISTAPRECIOS WHERE PRO_PRODUCTO.IDPRODUCTO = PRO_DETALLELISTAPRECIOS.IDPRODUCTO AND IDLISTA = :PIDLISTA"
            cmd.Parameters.Add("PIDLISTA", OracleDbType.Varchar2).Value = lListaPrecios
        Else
            cmd.CommandText = "SELECT * FROM PRO_PRODUCTO WHERE DESCRICION LIKE :PPRODUCTO"
        End If
        If Me.txtDescripcion.Text.Trim.Length > 0 Then
            cmd.CommandText &= " AND DESCRICION LIKE '%" & Me.txtDescripcion.Text & "%'"
        End If
        If lestado = 1 Or lestado = 2 Then
            cmd.CommandText &= "AND ESTADO=:PESTADO"
            cmd.Parameters.Add("PESTADO", OracleDbType.Int16).Value = lestado
        End If
        Try
            DsProductos.PRO_PRODUCTO.Rows.Clear()
            conn.Open()
            DsProductos.PRO_PRODUCTO.Load(cmd.ExecuteReader)
            PRO_PRODUCTODataGridView.Focus()
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub btnConsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsulta.Click
        Consultar()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
    Private Sub AsignaProducto(ByVal Fila As Integer)
        If Fila >= 0 Then
            lIdArticulo = PRO_PRODUCTODataGridView.Rows(Fila).Cells("IDPRODUCTO").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub
    Private Sub PRO_PRODUCTODataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles PRO_PRODUCTODataGridView.CellMouseDoubleClick
        AsignaProducto(e.RowIndex)
    End Sub

    Private Sub PRO_PRODUCTODataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles PRO_PRODUCTODataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaProducto(PRO_PRODUCTODataGridView.CurrentRow.Index)
        End If
    End Sub

    Private Sub fListaProductos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If lItemBusca IsNot Nothing AndAlso lItemBusca.Trim.Length > 0 Then
            Me.txtDescripcion.Text = lItemBusca
            Consultar()
        End If
    End Sub
End Class